﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;

namespace BouquetApplication
{
    public class ComponentValidator
    {
        private Dictionary<Component, List<IValidator>> validators;
        private Dictionary<Component, string> names;

        public ComponentValidator()
        {
            validators = new Dictionary<Component, List<IValidator>>();
            names = new Dictionary<Component, string>();
        }

        public string Validate()
        {
            foreach (Component component in validators.Keys)
            {
                List<IValidator> validatorList = validators[component];
                foreach (IValidator validator in validatorList)
                {
                    if (typeof(TextBox).IsAssignableFrom(component.GetType()))
                    {
                        TextBox textBox = (TextBox)component;
                        if (!validator.Validate(textBox.Text))
                        {
                            textBox.BorderStyle = new BorderStyle();
                            return validator.GetErrorMessage() + names[component];
                        }
                    }
                }
            }

            return null;
        }

        public void Add(Component component, IValidator validator, string componentName)
        {
             List<IValidator> validatorList;
            if (validators.ContainsKey(component))
            {
                validatorList = validators[component];
                validatorList.Add(validator);
            }
            else
            {
                validatorList = new List<IValidator>();
                validatorList.Add(validator);
                validators.Add(component, validatorList);
                names.Add(component, componentName);
            }         
        }
    }
}
